/* $Id: JGameMapPropertiesPanel.java 64 2010-02-14 13:53:27Z aaron.day $ */
package org.crazydays.gameplan.map.swing;


import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

import org.crazydays.gameplan.map.GameMap;
import org.crazydays.gameplan.map.GridType;
import org.crazydays.i18n.Messages;

/**
 * JGameMapPropertiesPanel
 */
public class JGameMapPropertiesPanel
    extends JPanel
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: JGameMapPropertiesPanel.java 64 2010-02-14 13:53:27Z aaron.day $"
            .hashCode();

    /** messages */
    public static Messages MESSAGES = Messages.getInstance();

    /** resource key: label name */
    public static String RESKEY_LABEL_NAME = "mapProperties.label.name";

    /** resource key: label grid type */
    public static String RESKEY_LABEL_GRID_TYPE =
        "mapProperties.label.gridType";

    /** resource key: label grid size */
    public static String RESKEY_LABEL_GRID_SIZE =
        "mapProperties.label.gridSize";

    /** resource key: label height */
    public static String RESKEY_LABEL_HEIGHT = "mapProperties.label.height";

    /** resource key: label width */
    public static String RESKEY_LABEL_WIDTH = "mapProperties.label.width";

    /** game map */
    protected GameMap gameMap;

    /** model */
    protected GameMapModel model;

    /** name label */
    protected JLabel nameLabel;

    /** name input */
    protected JTextField nameInput;

    /** type label */
    protected JLabel gridTypeLabel;

    /** type input */
    protected JComboBox gridTypeInput;

    /** size label */
    protected JLabel gridSizeLabel;

    /** size input */
    protected JSpinner gridSizeInput;

    /** height label */
    protected JLabel heightLabel;

    /** height input */
    protected JSpinner heightInput;

    /** width label */
    protected JLabel widthLabel;

    /** width input */
    protected JSpinner widthInput;

    /**
     * JGameMapPropertiesPanel constructor.
     * <p>
     * Use default game map properties.
     * </p>
     */
    public JGameMapPropertiesPanel()
    {
        this.gameMap = new GameMap("New Map", 600, 800, GridType.SQUARE, 20);
        this.model = new GameMapModel(gameMap);
    }

    /**
     * JGameMapPropertiesPanel constructor.
     * 
     * @param gameMap GameMap
     */
    public JGameMapPropertiesPanel(GameMap gameMap)
    {
        this.gameMap = gameMap;
        this.model = new GameMapModel(gameMap);
    }

    /**
     * Initialize the form components.
     */
    public void initialize()
    {
        initializeName();
        initializeGridType();
        initializeGridSize();
        initializeHeight();
        initializeWidth();
        initializeLayout();
    }

    /**
     * Initialize name.
     */
    protected void initializeName()
    {
        nameLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_NAME));
        nameInput = new JTextField(gameMap.getName());
        nameInput.getDocument().addDocumentListener(
            model.getNameDocumentListener());
    }

    /**
     * Initialize the map type components.
     */
    protected void initializeGridType()
    {
        gridTypeLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_GRID_TYPE));
        gridTypeInput =
            new JComboBox(model.getGeometryModel().getGridTypeModel());
    }

    /**
     * Initialize grid size components.
     */
    protected void initializeGridSize()
    {
        gridSizeLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_GRID_SIZE));
        gridSizeInput =
            new JSpinner(model.getGeometryModel().getGridSizeModel());
    }

    /**
     * Initialize map height components.
     */
    protected void initializeHeight()
    {
        heightLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_HEIGHT));
        heightInput = new JSpinner(model.getGeometryModel().getHeightModel());
    }

    /**
     * Initialize map width components.
     */
    protected void initializeWidth()
    {
        widthLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_WIDTH));
        widthInput = new JSpinner(model.getGeometryModel().getWidthModel());
    }

    /**
     * Initialize component layout.
     */
    protected void initializeLayout()
    {
        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        // horizontal
        layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
            layout.createParallelGroup().addComponent(nameLabel).addComponent(
                gridTypeLabel).addComponent(gridSizeLabel).addComponent(
                heightLabel).addComponent(widthLabel)).addGroup(
            layout.createParallelGroup().addComponent(nameInput).addComponent(
                gridTypeInput).addComponent(gridSizeInput).addComponent(
                heightInput).addComponent(widthInput)));

        // vertical
        layout.setVerticalGroup(layout.createSequentialGroup().addGroup(
            layout.createParallelGroup(Alignment.BASELINE).addComponent(
                nameLabel).addComponent(nameInput)).addGroup(
            layout.createParallelGroup(Alignment.BASELINE).addComponent(
                gridTypeLabel).addComponent(gridTypeInput)).addGroup(
            layout.createParallelGroup(Alignment.BASELINE).addComponent(
                gridSizeLabel).addComponent(gridSizeInput)).addGroup(
            layout.createParallelGroup(Alignment.BASELINE).addComponent(
                heightLabel).addComponent(heightInput)).addGroup(
            layout.createParallelGroup(Alignment.BASELINE).addComponent(
                widthLabel).addComponent(widthInput)));
    }

    /**
     * Get game map.
     * 
     * @return GameMap
     */
    public GameMap getGameMap()
    {
        return gameMap;
    }
}
